home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / docs / zed3d050 / siggraph.86 < prev   
Text File  |  1995-03-03  |  15KB  |  419 lines

  1. Fast Phong Shading. p 103-105
  2.  Gary Bishop
  3.  Room 4E-626
  4.  David M Weimer
  5.  Room 4F-637
  6.  AT&T Bell Laboratories
  7.  Crawfords Corner Road
  8.  Holmdel, NJ 07733
  9.  
  10.   Permission to copy without fee all or part of this material is granted
  11.   provided that the copies are not made or distributed for direct commer-
  12.   cial advantage, the ACM copyright notice and the title of the publication
  13.   and its date appear, and notice is given that copying is by permission
  14.   of the Association for Computer Machinery. To copy otherwise, or to
  15.   republish, requires a fee and/or specific permission.
  16.  
  17.   SIGGRAPH @1986 - ACM 0-89791-196-2/86/008/0103
  18.  
  19. Abstract:
  20.  
  21. Computer image generation systems often represent curved surfaces as a mesh
  22. of polygons that are shaded to restore a smooth appearance.  Phong shading
  23. is a well known algorithm for producing a realistic shading but it has not
  24. been used by real-time systems because of the 3 additions, 1 division and
  25. 1 square root required per pixel for its evaluation.  We describe a new
  26. formulation for Phong shading that reduces the amount of computation per
  27. pixel to only 2 additions for simple Lambertian reflection and 5 additions
  28. and 1 memory reference for Phong's complete reflection model. We also show
  29. how to extend our method to compute the specular component with the eye at
  30. a finite distance from the scene rather than at infinity as is usually
  31. assumed. The method can be implemented in hardware for real-time applications
  32. or in software to speed image generation for almost any system.
  33.  
  34. Introduction:
  35.  
  36. Most computer image generation (CIG) systems represent curved surfaces as a
  37. mesh of planar polygons because polygons can be transformed and rendered
  38. quickly with well known algorithms.  Since the polygonal representation is an
  39. artifact of the image generation method and is usually of no interest to
  40. viewers (see figure 1), these systems attempt to restore a smooth appearance
  41. to surfaces by varying the intensity across polygons.  The efficiency of this
  42. shading operation is critical to the performance of a CIG system because it
  43. must be performed for one million or more pixels per image.  Although
  44. algorithms for realistic shading of polygons are well known, real-time CIG
  45. systems have not used them because of the large amount of computation they
  46. require per pixel.  This paper describes a new formulation of Phong shading
  47. that drastically reduces the amount of computation compared to previously
  48. known formulations. While the new formulation is well suited to implementation
  49. with special hardware for real-time display, it is also appropriate for
  50. implementation with software for a variety of display systems. Readers who are
  51. unfamiliar with surface representation or shading are referred to one of the
  52. standard graphics texts for more information, [Newman and Sproul, 1979; Foley
  53. and Van Dam, 1983].
  54.  
  55. Background:
  56.  
  57. Assume, for simplicity, that the input to the shader consists of triangles,
  58. in screen coordinates, with normals, N, to the original curved surface
  59. specified at each vertex.  The formulations can be extended to polygons with
  60. four or more sides.
  61.  
  62. The shading algorithms must determine the intensity of each point within a
  63. triangle from the surface normals, and a vector to the light source, L.  The
  64. light source is assumed to be at infinity so that L is independent of the
  65. point on which the light shines.  We will start with diffuse reflection,
  66.  
  67.                    L * N
  68.     I        = -------------,
  69.      diffuse     |L| * |N|
  70.  
  71.  
  72. and later show how to extend the results to include Phong's highlight model
  73. and an extended highlight model.
  74.  
  75. GOURAUD SHADING: the most commonly used shading method in real-time systems.
  76. Gouraud shading [Gouraud, 1971], computes the intensity at each point by
  77. linear interpolation of the intensities at the vertices. The intensities are
  78. determined using the reflection equation above with normals given at the
  79. vertices. The method is popular for real-time systems because it produces
  80. images of acceptable quality with only 1 addition per pixel, but the shading
  81. has several disturbing characteristics. (1) Moving objects tend to "flatten
  82. out" at certain viewing angles, (2) surfaces appear dull or chalky, and (3)
  83. images show pronounced Mach bands, exaggerations of intensity change at
  84. discontinuities.  Figure 2 is a Gouraud shaded chess piece.
  85.  
  86. PHONG SHADING: Phong shading, [Phong, 1973], eliminates "flattening out" and
  87. dull surfaces and reduces Mach bands but has not, to my knowledge, been used
  88. in any real-time system because of the large amount of computation required
  89. for its usual formulation.  Phong's method determines the intensity of each
  90. point using an approximate surface normal that is linearly interpolated from
  91. the true surface normals specified at the vertices,
  92.  
  93.   N(x,y) = Ax + By + C
  94.  
  95. where A, B and C are chosen to interpolate the normal across the polygon.
  96. Interpolation for successive values of x and y and evaluating the illumination
  97. model requires 7 additions, 6 multiplications, 1 division and 1 square root
  98. per pixel.  Phong proposed a complicated circuit for evaluating this function
  99. but it has not, to my knowledge, been implemented.  Figure 3 is a Phong shaded
  100. chess piece.
  101.  
  102. Tom Duff has shown, in [Duff, T.,1979, "Smoothly Shaded Renderings of
  103. Polyhedral Objects on Raster Displays", ACM Computer Graphics, vol 13, no 2,
  104. pp 270-275]], that Phong shading can be implemented more efficiently by
  105. combining the interpolation and reflection equations.
  106.  
  107.                         L        Ax + By + C
  108.        I       (x,y) = --- * -------------------
  109.         diffuse        |L|    |L| |Ax + By + C|
  110.  
  111.  
  112. Which can be rewritten as
  113.  
  114.                           L*Ax + L*By + L*C
  115.        I       (x,y) = ------------------------
  116.         diffuse        |L| |L*Ax + L*By + L*C|
  117.  
  118. Performing the indicated dot products and expanding the vector magnitude
  119. yields:
  120.  
  121.                                     ax + by + c
  122.        I       (x,y) = --------------------------------------
  123.         diffuse        SQRT( dx^2 + exy + fy^2 + gx + hy + i)
  124.  
  125. with
  126.              L * A
  127.          a = ----- ,
  128.               |L|
  129.  
  130.              L * B
  131.          b = ----- ,
  132.               |L|
  133.  
  134.              L * C
  135.          c = ----- ,
  136.               |L|
  137.  
  138.          d = A * A,
  139.  
  140.          e = 2A * B,
  141.  
  142.          f = B * B,
  143.  
  144.          g = 2A * C,
  145.  
  146.          h = 2B * C, and
  147.  
  148.          i = C * C.
  149.  
  150.  
  151. Using forward differences, this form can be evaluated for successive values
  152. of x and y with 3 additions, 1 division and 1 square root per pixel. This is
  153. a substantial savings over Phong's formulation but the division and square
  154. root are still too expensive for real-time use.
  155.  
  156. A New Approach:
  157.  
  158. For display purposes we need not evaluate the reflection equation exactly;
  159. a good approximation will suffice.  The "error" introduced by the
  160. approximation will be of no consuquence since Phong's normal interpolation and
  161. the reflection equation are already approximations.  the one-dimensional form
  162. of Taylor's series is widely used for approximation functions, such as sine
  163. and log, with polynomials.  The less widely used, two-dimensional form will
  164. serve to approximate the reflection equation.
  165.  
  166. Taylor's series for a function of two variables is:
  167.  
  168. f(a+h, b+k) = f(a,b) + (h ... + k ...)*f(a,b) + ....... [look it up!]
  169.  
  170. Shifting the triangle so that (0,0) lies at its center, and expanding in a
  171. Taylor series to the second degree produces
  172.  
  173.  
  174.        I       (x,y) = T x^2 + T xy + T y^2 + T x + T y + T
  175.         diffuse         5       4      3       2     1     0
  176.  
  177. with
  178.  
  179.         3ig^2 - 4cdi - 4agi
  180.   T  = ---------------------,
  181.    5     8 * i^2 * SQRT(i)
  182.  
  183.  
  184.         3cgh - 2cei - 2bgi - 2ahi
  185.   T  = ---------------------------,
  186.    4        4 * i^2 * SQRT(i)
  187.  
  188.  
  189.         3ch^2 - 4cfi - 4bhi
  190.   T  = ---------------------,
  191.    3     8 * i^2 * SQRT(i)
  192.  
  193.  
  194.            2ai - cg
  195.   T  = -----------------,
  196.    2    2 * i * SQRT(i)
  197.  
  198.  
  199.            2bi - ch
  200.   T  = -----------------, and
  201.    1    2 * i * SQRT(i)
  202.  
  203.  
  204.  
  205.            c
  206.   T  = ---------
  207.    0    SQRT(i)
  208.  
  209.  
  210. Using this expansion and forward differences, the intensity can be evaluated
  211. with only 2 additions per pixel.
  212.  
  213. The method can be extended to handle Phong's specular reflection model,
  214.                   n
  215. I        = (N * H) , by using Taylor's series to evaluate the dot product
  216.  specular            and table lookup to do the exponentiation (A table of
  217.                      1K bytes will allow exponentiation to powers up to 20
  218. with less than 1 percent error and a table of 8K bytes allows powers up to
  219. 164.)  H is a vector in the direction of maximum highlight which is halfway
  220. between the light direction, L, and the eye direction, E,
  221.  
  222.                  E * L
  223.             H = -------,
  224.                 |E + L|
  225.  
  226. The eye, like the light source, is assumed for the present to be at infinity.
  227. Phong's reflection equation, I = I       + I       +  I        , can now be
  228.                                   ambient   diffuse    specular
  229. evaluated with only 5 additions and 1 memory access per pixel (the ambient
  230. component can be rolled into the series for the diffuse component and the
  231. series for the specular component can be scaled to allow direct addressing of
  232. the exponentiation table).  It should be simple to configure hardware to do
  233. these operations in less than 100 nanoseconds per pixel.
  234.  
  235. The additional computation required to determine the Ti for our new method is
  236. offset by the greatly reduced computation per pixel for polygons with 10 or
  237. more pixels.  This estimate is based on the following assumptions: (a) the
  238. algorithms are implemented on sequential processors of conventional design,
  239. (b) with modern hardware, the time for floating point addition,
  240. multiplication, and memory references are all about equal, (c) the
  241. computation of 1 / SQRT(x) requires about 10 operations, and (d) common
  242. subexpressions have been removed from the formulas for Ti. Timings and
  243. analysis of the method as implementd in the raster testbed [Whitted and
  244. Weimer, 1982] also support a 10 pixel break-even point. The break-even point
  245. would be much lower if some simple hardware were added to do the additions
  246. and table lookup for our method in parallel.  Of course, special hardware
  247. could be built for the other methods but it would be much more complicated
  248. and probably not as fast.
  249.  
  250. The images we have produced with this approximation are indistinguishable
  251. from those produced with Phong's method.  For polygons with more than about
  252. 60 degrees of curvature, this new expansion will produce shadings that are
  253. noticeably different from Phong shading but curvatures this large should
  254. never be represented by a single polygon.
  255.  
  256.  
  257. Going Further:
  258.  
  259. Since the form of the polynomial we have to evaluate at each pixel is
  260. independent of the original reflection equation, we can use more elaborate
  261. models. One useful extension is to provide for finite eye distance, thus
  262. requiring that the vector to the eye, E, vary across the scene. The resulting
  263. variation in the specular component produces more natural looking illumination
  264. for scenes rendered with perspective. This is most obvious when there are
  265. planar surfaces in the scene because Phong shading with the eye at infinity,
  266. like flat shading, renders all parallel surfaces with the same intensity.
  267. Figure 4 is a comparision of Phong shading with the eye at infinity and with
  268. the eye at a true perspective distance. The reflection equation for the
  269. specular component with the eye at a finite distance now becomes
  270.  
  271.                 N(x,y) * H(x,y)
  272.   I        = ---------------------,
  273.    specular   |N(x,y)| * |H(x,y)|
  274.  
  275.  
  276. where H(x,y) = E(x,y) + L and E(x,y) interpolates the eye vector across the
  277. triangle. This can be expanded just as before
  278.  
  279.                 (Ax + By + C) * (Dx + Ey + F)
  280.   I        = -----------------------------------
  281.    specular   |(Ax + By + C)| * |(Dx + Ey + F)|
  282.  
  283.  
  284. After performing the dot products and expanding the vector magnitude as
  285. before
  286.  
  287.                        ax^2 + bxy + cy^2 + dx + ey + f
  288. I =--------------------------------------------------------------------------
  289.  s  SQRT((gx^2 + hxy + iy^2 + jx + ky + l)*(mx^2 + nxy + oy^2 + px + qy + r))
  290.  
  291. with
  292.  
  293.   a = A * D,
  294.  
  295.   b = A * E + B * D,
  296.  
  297.   c = B * E,
  298.  
  299.   d = A * F + C * D,
  300.  
  301.   e = B * F + C * E,
  302.  
  303.   f = C * F,
  304.  
  305.   g = A * A,
  306.  
  307.   h = 2A * B,
  308.  
  309.   i = B * B,
  310.  
  311.   j = 2A * C,
  312.  
  313.   k = 2B * C,
  314.  
  315.   l = C * C,
  316.  
  317.   m = E * E,
  318.  
  319.   n = D * D,
  320.  
  321.   o = 2D * E,
  322.  
  323.   p = 2D * F,
  324.  
  325.   q = 2E * F, and
  326.  
  327.   r = F * F.
  328.  
  329. And expanding in Taylor's series about (0,0).
  330.  
  331.   I(x,y) = T x^2 + T xy + T y^2 + T x + T y + T
  332.             5       4      3       2     1     0
  333.  
  334. with
  335.  
  336.      8al^2r^2 - 4djlr^2 - 4fglr^2 + 3fj^2r^2 - 4dl^2pr + 2fjlpr - 4fl^2mr 
  337. T = ----------------------------------------------------------------------
  338.  5                           8l^2r^2 * SQRT(l*r)
  339.  
  340.           + 3fl^2p^2
  341.     + ---------------------,
  342.        8l^2r^2 * SQRT(l*r)
  343.  
  344.  
  345.      4bl^2r^2 - 2dklr^2 - 2ejlr^2 - 2fhlr^2 + 3fjkr^2 - 2dl^2qr 
  346. T = ------------------------------------------------------------
  347.  4                       4l^2r^2 * SQRT(l*r)
  348.  
  349.         - fjlqr - 2el^2pr + fklpr - 2f^2nr + 3fl^2pq  
  350.     + ------------------------------------------------,
  351.                       4l^2r^2 * SQRT(l*r)
  352.  
  353.  
  354.      8cl^2r^2 - 4eklr^2 - 4filr^2 + 3fk^2r^2 - 4el^2qr + 2fklqr - 4fl^2or 
  355. T = ----------------------------------------------------------------------
  356.  3                           8l^2r^2 * SQRT(l*r)
  357.  
  358.           + 3fl^2q^2
  359.     + ---------------------,
  360.        8l^2r^2 * SQRT(l*r)
  361.  
  362.  
  363.  
  364.       2dlr - fjr - flp
  365. T = -------------------,
  366.  2    2lr * SQRT(l*r)
  367.  
  368.  
  369.       2elr - fkr - flq
  370. T = -------------------,
  371.  1    2lr * SQRT(l*r)
  372.  
  373.  
  374.          f
  375. T = -----------
  376.  0   SQRT(l*r)
  377.  
  378.  
  379.  
  380.  
  381. Summary:
  382.  
  383. We have shown that computer image generation systems can use Phong shading
  384. with only a little more computation per pixel than is required for Gouraud
  385. shading. This result should allow real-time systems to generate more
  386. realistic scenes and conventional systems to produce images more rapidly.
  387.  
  388. To test the method, we have implemented it as a shader subroutine for the
  389. raster testbed. The cpu time for rendering figure 4 with 14620 polygons
  390. at 2048x2048 resolutions on a DEC microVAX-II are:
  391.  
  392. Shading method               CPU time     Notes
  393. Gouraud                        406s       
  394. Taylor (infinite)              767s       incl 119s for Taylor coeffs
  395. Taylor (finite)                850s       incl 202s for Taylor coeffs
  396. Duff                          2597s       incl 1420 in sqrt
  397. Phong                         3900s       incl 1405 in sqrt
  398.  
  399.  
  400. Acknowledgements:
  401.  
  402.  
  403. Tom Duff and the reviewers provided many helpful suggestions.
  404.  
  405.  
  406.  
  407.  
  408. -----------------------------------EOF--------------------------------------
  409. Errata:
  410.  
  411.  T4 (p 105)
  412.  
  413.  - f j l q r      =>   + f j l q r
  414.  - 2 f^2 n r      =>   - 2 f l^2 n r
  415.  
  416.  
  417.  
  418.  
  419.